home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / jpeg / lib / jdmaster.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-25  |  5.2 KB  |  172 lines

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains the main control for the JPEG decompressor.
  9.  * The system-dependent (user interface) code should call jpeg_decompress()
  10.  * after doing appropriate setup of the decompress_info_struct parameter.
  11. %
  12. % Modified:     Jin Guojun - LBL, Image Technology Group
  13. %       Date:   April 14, 1992
  14. %       Goal:   To be easily handled by conversion library - CCS (c)
  15. %        HIPS, FITS, GIF, RLE, SUN-raster, PNM, TIFF, PICT ...
  16. %        can be compressed by cjpeg now, directly displayed by tuner,
  17. %        decompressed to other image type by torle, torast, and color_ps.
  18. %        These type of images can be determined by program `headers'.
  19. */
  20.  
  21. #include "jinclude.h"
  22.  
  23.  
  24. METHODDEF void
  25. d_per_scan_method_selection (decompress_info_ptr cinfo)
  26. /* Central point for per-scan method selection */
  27. {
  28.   /* MCU disassembly */
  29.   jseldmcu(cinfo);
  30.   /* Upsampling of pixels */
  31.   jselupsample(cinfo);
  32. }
  33.  
  34.  
  35. void
  36. d_initial_method_selection (decompress_info_ptr cinfo)
  37. /* Central point for initial method selection (after reading file header) */
  38. {
  39.   /* JPEG file scanning method selection is already done. */
  40.   /* So is output file format selection (both are done by user interface). */
  41.  
  42.   /* Entropy decoding: either Huffman or arithmetic coding. */
  43. #ifdef C_ARITH_CODING_SUPPORTED
  44.   jseldarithmetic(cinfo);
  45. #else
  46.   if (cinfo->arith_code) {
  47.     ERREXIT(cinfo->emethods, "Arithmetic coding not supported");
  48.   }
  49. #endif
  50.   jseldhuffman(cinfo);
  51.   /* Cross-block smoothing */
  52. #ifdef BLOCK_SMOOTHING_SUPPORTED
  53.   jselbsmooth(cinfo);
  54. #else
  55.   cinfo->do_block_smoothing = FALSE;
  56. #endif
  57.   /* Gamma and color space conversion */
  58.   jseldcolor(cinfo);
  59.  
  60.   /* Color quantization selection rules */
  61. #ifdef QUANT_1PASS_SUPPORTED
  62. #ifdef QUANT_2PASS_SUPPORTED
  63.   /* We have both, check for conditions in which 1-pass should be used */
  64.   if (cinfo->num_components != 3 || cinfo->jpeg_color_space != CS_YCbCr)
  65.     cinfo->two_pass_quantize = FALSE; /* 2-pass only handles YCbCr input */
  66.   if (cinfo->out_color_space == CS_GRAYSCALE)
  67.     cinfo->two_pass_quantize = FALSE; /* Should use 1-pass for grayscale out */
  68. #else /* not QUANT_2PASS_SUPPORTED */
  69.   cinfo->two_pass_quantize = FALSE; /* only have 1-pass */
  70. #endif
  71. #else /* not QUANT_1PASS_SUPPORTED */
  72. #ifdef QUANT_2PASS_SUPPORTED
  73.   cinfo->two_pass_quantize = TRUE; /* only have 2-pass */
  74. #else /* not QUANT_2PASS_SUPPORTED */
  75.   if (cinfo->quantize_colors) {
  76.     ERREXIT(cinfo->emethods, "Color quantization was not compiled");
  77.   }
  78. #endif
  79. #endif
  80.  
  81. #ifdef QUANT_1PASS_SUPPORTED
  82.   jsel1quantize(cinfo);
  83. #endif
  84. #ifdef QUANT_2PASS_SUPPORTED
  85.   jsel2quantize(cinfo);
  86. #endif
  87.  
  88.   /* Pipeline control */
  89.   jseldpipeline(cinfo);
  90.   /* Overall control (that's me!) */
  91.   cinfo->methods->d_per_scan_method_selection = d_per_scan_method_selection;
  92. }
  93.  
  94.  
  95. void
  96. initial_setup (decompress_info_ptr cinfo)
  97. /* Do computations that are needed before initial method selection */
  98. {
  99.   short ci;
  100.   jpeg_component_info *compptr;
  101.  
  102.   /* Compute maximum sampling factors; check factor validity */
  103.   cinfo->max_h_samp_factor = 1;
  104.   cinfo->max_v_samp_factor = 1;
  105.   for (ci = 0; ci < cinfo->num_components; ci++) {
  106.     compptr = &cinfo->comp_info[ci];
  107.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  108.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  109.       ERREXIT(cinfo->emethods, "Bogus sampling factors");
  110.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  111.                    compptr->h_samp_factor);
  112.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  113.                    compptr->v_samp_factor);
  114.  
  115.   }
  116.  
  117.   /* Compute logical downsampled dimensions of components */
  118.   for (ci = 0; ci < cinfo->num_components; ci++) {
  119.     compptr = &cinfo->comp_info[ci];
  120.     compptr->true_comp_width = (cinfo->image_width * compptr->h_samp_factor
  121.                 + cinfo->max_h_samp_factor - 1)
  122.                 / cinfo->max_h_samp_factor;
  123.     compptr->true_comp_height = (cinfo->image_height * compptr->v_samp_factor
  124.                  + cinfo->max_v_samp_factor - 1)
  125.                  / cinfo->max_v_samp_factor;
  126.   }
  127. }
  128.  
  129.  
  130. /*
  131.  * This is the main entry point to the JPEG decompressor.
  132.  */
  133.  
  134.  
  135. jpeg_decompress (decompress_info_ptr cinfo)
  136. {
  137. int    i = cinfo->img->frames;
  138.     do    {
  139.     read_next_jpeg_frame(cinfo);
  140.     if (cinfo->img->o_type == HIPS)
  141.         fwrite(cinfo->img->src, cinfo->img->dpy_channels,
  142.             cinfo->img->width*cinfo->img->height, cinfo->img->OUT_FP);
  143.     } while (--i > 0);
  144.     close_jpeg_read(cinfo);
  145. }
  146.  
  147. read_next_jpeg_frame(decompress_info_ptr cinfo)
  148. {
  149. int    orgi_passes = cinfo->total_passes;
  150.     if (feof(cinfo->input_file))    return    EOF;
  151.   /* And let the pipeline controller do the rest. */
  152.     (*cinfo->methods->d_pipeline_controller) (cinfo);
  153.     (*cinfo->methods->read_file_trailer) (cinfo);
  154.     cinfo->total_passes = orgi_passes;
  155.     cinfo->completed_passes = 0;
  156.     cinfo->methods->put_pixel_rows(cinfo, 0, 0);
  157. return    cinfo->image_height * cinfo->image_width;
  158. }
  159.  
  160. close_jpeg_read(decompress_info_ptr cinfo)
  161. {
  162.   /* Finish output file, release working storage, etc */
  163.   if (cinfo->quantize_colors)
  164.     (*cinfo->methods->color_quant_term) (cinfo);
  165.   (*cinfo->methods->colorout_term) (cinfo);
  166.   (*cinfo->methods->output_term) (cinfo);
  167.  
  168.   (*cinfo->emethods->free_all) ();
  169.  
  170.   /* My, that was easy, wasn't it? */
  171. }
  172.